using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Rilisoft.MiniJson;
using UnityEngine;

public class PromoActionManager : MonoBehaviour
{
	public class BannerInfo
	{
		public bool enabled;

		public List<string> imageUrls = new List<string>();

		public List<string> adUrls = new List<string>();

		public List<int> countCash = new List<int>();

		public List<int> countGold = new List<int>();

		public List<bool> showAgainList = new List<bool>();

		public int newUserStatusDays;

		public int showFrequency;

		public int ShowTimesTotal { get; set; }

		public int showAgainIndex { get; set; }

		public bool ShowToPaying { get; set; }

		public bool ShowToNew { get; set; }
	}

	public class FyberInfo
	{
		public bool enabled;

		public int ShowTimesTotal;

		public int timeDelay;

		public int showFrequency;

		public bool ShowToPaying;

		public bool ShowToNew;

		public List<string> rewardType = new List<string>();

		public List<int> reward = new List<int>();

		public bool lobby;

		public bool pvpMaps;

		public bool loadAdventure;

		public bool loadSandbox;

		public bool pvpMatchEnd;

		public bool leavePvp;

		public bool leaveSandbox;

		public bool leaveAdventure;

		public bool transilitionToThePost;

		public int newUserStatusHours;

		public int newUserStatusDays;

		public bool rewardedVideoLaunchRequest;

		public bool rewardedVideoOpenTabRequest;

		public int interstitialPreRequestTime;

		public int interstitialRequestCount;

		public int interstitialDelayBetweenRequests;

		public int maxTimeShowInterstitial;

		public int minTimeShowInterstitial;

		public int timeForShowMaxTimeInterstitial;

		public int secondsToShowX3;

		public int secondsAfterPauseToShowX3;
	}

	private bool _isGetAdvertInfoRunning;

	private static BannerInfo _replace = new BannerInfo();

	private static FyberInfo _fyber = new FyberInfo();

	public static bool jsonReaded = false;

	public static BannerInfo ReplacePereliv
	{
		get
		{
			return _replace;
		}
	}

	public static FyberInfo fyber
	{
		get
		{
			return _fyber;
		}
	}

	public static int CurrentUnixTimeSecond
	{
		get
		{
			DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
			return (int)(DateTime.UtcNow - dateTime).TotalSeconds;
		}
	}

	public void Awake()
	{
	}

	private void Start()
	{
	}

	public static bool PayingUser()
	{
		return FlurryWrapper.IsPayingUser();
	}

	public static bool NewUser(int hours)
	{
		int newUserStatusDays = fyber.newUserStatusDays;
		int @int = PlayerPrefs.GetInt(settings.keyTimeStartNewUser);
		if (@int == 0)
		{
			PlayerPrefs.SetInt(settings.keyTimeStartNewUser, CurrentUnixTimeSecond);
			return true;
		}
		double result;
		double.TryParse(BCWStorage.Get<string>(settings.keyTotalTimePlayed), out result);
		double value = result + (double)Time.time;
		int seconds = (int)TimeSpan.FromSeconds(value).TotalSeconds;
		if (CurrentUnixTimeDay(CurrentUnixTimeSecond - @int) >= newUserStatusDays || CurrentUnixTimeHour(seconds) >= hours)
		{
			return false;
		}
		return true;
	}

	public static bool PlayedHours(int hours)
	{
		if (hours <= 0)
		{
			return true;
		}
		double result;
		double.TryParse(BCWStorage.Get<string>(settings.keyTotalTimePlayed), out result);
		result += (double)Time.time;
		int seconds = (int)TimeSpan.FromSeconds(result).TotalSeconds;
		return CurrentUnixTimeHour(seconds) >= hours;
	}

	public static int CurrentUnixTimeDay(int seconds = 0)
	{
		if (seconds == 0)
		{
			seconds = CurrentUnixTimeSecond;
		}
		return seconds / 86400;
	}

	public static int CurrentUnixTimeHour(int seconds = 0)
	{
		if (seconds == 0)
		{
			seconds = CurrentUnixTimeSecond;
		}
		return seconds / 3600;
	}

	public void GetAdvertInfo()
	{
		StartCoroutine(GetAdvertInfoCoroutine());
	}

	private IEnumerator GetAdvertInfoCoroutine()
	{
		if (_isGetAdvertInfoRunning)
		{
			yield break;
		}
		_isGetAdvertInfoRunning = true;
		if (string.IsNullOrEmpty(URLs.Advert))
		{
			_isGetAdvertInfoRunning = false;
			yield break;
		}
		string ss = URLs.Advert;
		BCWDebug.Log(ss, "Fyber");
		WWW response = new WWW(URLs.Advert);
		yield return response;
		if (!string.IsNullOrEmpty(response.error))
		{
			BCWDebug.Log("Advert response error: " + response.error, "Fyber");
			_isGetAdvertInfoRunning = false;
			yield break;
		}
		string responseText = response.text;
		if (string.IsNullOrEmpty(responseText))
		{
			BCWDebug.Log("Advert response is empty", "Fyber");
			_isGetAdvertInfoRunning = false;
			yield break;
		}
		object advertInfoObj = Json.Deserialize(responseText);
		Dictionary<string, object> advertInfo = advertInfoObj as Dictionary<string, object>;
		if (advertInfoObj == null)
		{
			_isGetAdvertInfoRunning = false;
			yield break;
		}
		if (advertInfo.ContainsKey("replace_admob_pereliv"))
		{
			Dictionary<string, object> replaceAdmob2 = advertInfo["replace_admob_pereliv"] as Dictionary<string, object>;
			ParseBanner(replaceAdmob2, _replace);
		}
		else
		{
			BCWDebug.Log("Advert response doesn't contain “replace_admob_pereliv” property.", "Fyber");
		}
		if (advertInfo.ContainsKey("fyber"))
		{
			Dictionary<string, object> replaceAdmob = advertInfo["fyber"] as Dictionary<string, object>;
			ParseFyber(replaceAdmob, fyber);
		}
		else
		{
			BCWDebug.Log("Advert response doesn't contain “fyber” property.", "Fyber");
		}
		_isGetAdvertInfoRunning = false;
		jsonReaded = true;
		if (ReplacePerelivController.instance != null)
		{
			ReplacePerelivController.instance.Init();
		}
		FyberWrapper.instance.CheckRVAvailability();
		if (FyberWrapper.firstShowDelay && FyberWrapper.instance != null)
		{
			StartCoroutine(FyberWrapper.instance.FirstShowDelay());
		}
	}

	private static void ParseBanner(Dictionary<string, object> replace, BannerInfo replaceObj)
	{
		if (replace != null)
		{
			List<string> list = (replace["countCash"] as List<object>).OfType<string>().ToList();
			replaceObj.countCash.Clear();
			foreach (string item in list)
			{
				try
				{
					replaceObj.countCash.Add(Convert.ToInt32(item));
				}
				catch
				{
					replaceObj.countCash.Add(0);
				}
			}
			BCWDebug.Log(replaceObj.countCash[0] + " cash", "Fyber");
			List<string> list2 = (replace["countGold"] as List<object>).OfType<string>().ToList();
			replaceObj.countGold.Clear();
			foreach (string item2 in list2)
			{
				try
				{
					replaceObj.countGold.Add(Convert.ToInt32(item2));
				}
				catch
				{
					replaceObj.countGold.Add(0);
				}
			}
			BCWDebug.Log(replaceObj.countGold[0] + " gold", "Fyber");
			List<string> list3 = (replace["showAgain"] as List<object>).OfType<string>().ToList();
			replaceObj.showAgainList.Clear();
			foreach (string item3 in list3)
			{
				replaceObj.showAgainList.Add(item3 == "1");
			}
			try
			{
				replaceObj.imageUrls = (replace["imageUrls"] as List<object>).OfType<string>().ToList();
				replaceObj.adUrls = (replace["adUrls"] as List<object>).OfType<string>().ToList();
				replaceObj.enabled = (long)replace["enabled"] == 1;
				replaceObj.showAgainIndex = Mathf.Max((int)(long)replace["showAgainIndex"], -1);
				replaceObj.ShowTimesTotal = Mathf.Max((int)(long)replace["showTimesTotal"], 0);
				replaceObj.ShowToPaying = (long)replace["showToPaying"] == 1;
				replaceObj.ShowToNew = (long)replace["showToNew"] == 1;
				replaceObj.showFrequency = Mathf.Max((int)(long)replace["showFrequency"], -1);
				replaceObj.newUserStatusDays = Mathf.Max((int)(long)replace["newUserStatusDays"], -1);
				return;
			}
			catch
			{
				BCWDebug.Log("replace_admob_pereliv exception whiel parsing", "Fyber");
				return;
			}
		}
		BCWDebug.Log("replaceAdmob == null", "Fyber");
	}

	private static void ParseFyber(Dictionary<string, object> replace, FyberInfo replaceObj)
	{
		if (replace != null)
		{
			List<string> list = (replace["reward"] as List<object>).OfType<string>().ToList();
			replaceObj.reward.Clear();
			foreach (string item in list)
			{
				try
				{
					replaceObj.reward.Add(Convert.ToInt32(item));
				}
				catch
				{
					replaceObj.reward.Add(0);
				}
			}
			List<string> list2 = (replace["rewardType"] as List<object>).OfType<string>().ToList();
			replaceObj.rewardType.Clear();
			foreach (string item2 in list2)
			{
				try
				{
					replaceObj.rewardType.Add(item2);
				}
				catch
				{
					replaceObj.rewardType.Add(string.Empty);
				}
			}
			try
			{
				replaceObj.enabled = (long)replace["enabled"] == 1;
				replaceObj.ShowTimesTotal = Mathf.Max((int)(long)replace["showTimesTotal"], 0);
				replaceObj.ShowToPaying = (long)replace["showToPaying"] == 1;
				replaceObj.ShowToNew = (long)replace["showToNew"] == 1;
				replaceObj.showFrequency = Mathf.Max((int)(long)replace["showFrequency"], -1);
				replaceObj.timeDelay = Mathf.Max((int)(long)replace["timeDelay"], -1);
				replaceObj.lobby = (long)replace["lobby"] == 1;
				replaceObj.pvpMaps = (long)replace["pvpMaps"] == 1;
				replaceObj.loadAdventure = (long)replace["loadAdventure"] == 1;
				replaceObj.loadSandbox = (long)replace["loadSandbox"] == 1;
				replaceObj.pvpMatchEnd = (long)replace["pvpMatchEnd"] == 1;
				replaceObj.leavePvp = (long)replace["leavePvp"] == 1;
				replaceObj.leaveSandbox = (long)replace["leaveSandbox"] == 1;
				replaceObj.leaveAdventure = (long)replace["leaveAdventure"] == 1;
				replaceObj.transilitionToThePost = (long)replace["transilitionToThePost"] == 1;
				replaceObj.rewardedVideoLaunchRequest = (long)replace["rewardedVideoLaunchRequest"] == 1;
				replaceObj.rewardedVideoOpenTabRequest = (long)replace["rewardedVideoOpenTabRequest"] == 1;
				replaceObj.interstitialPreRequestTime = Mathf.Max((int)(long)replace["interstitialPreRequestTime"], -1);
				replaceObj.interstitialRequestCount = Mathf.Max((int)(long)replace["interstitialRequestCount"], -1);
				replaceObj.interstitialDelayBetweenRequests = Mathf.Max((int)(long)replace["interstitialDelayBetweenRequests"], -1);
				replaceObj.secondsToShowX3 = Mathf.Max((int)(long)replace["secondsToShowX3"], -1);
				replaceObj.secondsAfterPauseToShowX3 = Mathf.Max((int)(long)replace["secondsAfterPauseToShowX3"], -1);
				replaceObj.maxTimeShowInterstitial = Mathf.Max((int)(long)replace["maxTimeShowInterstitial"], -1);
				replaceObj.minTimeShowInterstitial = Mathf.Max((int)(long)replace["minTimeShowInterstitial"], -1);
				replaceObj.timeForShowMaxTimeInterstitial = Mathf.Max((int)(long)replace["timeForShowMaxTimeInterstitial"], -1);
				replaceObj.newUserStatusHours = Mathf.Max((int)(long)replace["newUserStatusHours"], -1);
				replaceObj.newUserStatusDays = Mathf.Max((int)(long)replace["newUserStatusDays"], -1);
				return;
			}
			catch
			{
				BCWDebug.Log("exception whiel parsing", "Fyber");
				return;
			}
		}
		BCWDebug.Log("replaceAdmob == null", "Fyber");
	}
}
